# -*- coding:utf-8 -*-

'''
PROJECT_NAME : websocket_schedule_with_flask
file    : models
author  : 1032162439@qq.com
date    : 2022-10-26 11:46
IDE     : PyCharm
'''
import datetime

from . import db


class User(db.Model):
    """
    User
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32))
    password = db.Column(db.String(64))
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    line_manager_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    line_manager = db.relationship("User", remote_side=[id], backref=db.backref('members', lazy='dynamic'))
    role = db.relationship('Role', backref=db.backref('users', lazy='dynamic'))

    def __init__(self, name, password):
        self.name = name
        self.password = password

    def __repr__(self):
        return f'<User:{self.__class__.__name__}>---{self.name}'

    def to_dict(self, fields=None, exclude=None):
        all = ['id', 'name', 'password', 'role_id', 'role']
        data = {}
        for item in all:
            data[item] = getattr(self, item)
        if fields:
            pass
        if exclude:
            pass
        return data


class Role(db.Model):
    """
    Role
    """
    id = db.Column(db.Integer, primary_key=True)
    role_name = db.Column(db.String(32))

    def __init__(self, role_name):
        self.role_name = role_name

    def __repr__(self):
        return f'<{self.__class__.__name__}>---{self.role_name}'


class Action(db.Model):
    """
    Action
    """
    id = db.Column(db.Integer, primary_key=True)
    action_name = db.Column(db.String(16))


class Status(db.Model):
    """
    Status
    """
    id = db.Column(db.Integer, primary_key=True)
    status_name = db.Column(db.String(16))


class StatusTransfer(db.Model):
    """
    StatusTransfer
    """
    id = db.Column(db.Integer, primary_key=True)
    action_id = db.Column(db.Integer, db.ForeignKey('action.id'))
    current_status_id = db.Column(db.Integer, db.ForeignKey('status.id'))
    next_status_id = db.Column(db.Integer, db.ForeignKey('status.id'))

    action = db.relationship('Action', backref="status_transfer")

    current_status = db.relationship('Status', backref="current_status_t", foreign_keys=[current_status_id])
    next_status = db.relationship('Status', backref="next_status_t", foreign_keys=[next_status_id])


class ItemClass(db.Model):
    """
    ItemClass 工单类型
    """
    id = db.Column(db.Integer, primary_key=True)
    item_cls_name = db.Column(db.String(16))


class Item(db.Model):
    """
    Item
    工单编号
    工单类型
    工单状态
    创建时间
    修改时间
    待操作人员
    备注
    """
    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.String(36))
    order_type_id = db.Column(db.Integer, db.ForeignKey('item_class.id'))
    order_status_id = db.Column(db.Integer, db.ForeignKey('status.id'))
    create_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    active_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    comments = db.Column(db.String(256))

    order_type = db.relationship('ItemClass', backref='items')
    order_status = db.relationship('Status', backref='items')
    create_user = db.relationship('User', backref='items_create', foreign_keys=[create_user_id])
    active_user = db.relationship('User', backref='items_active', foreign_keys=[active_user_id])

    create_time = db.Column(db.DateTime, default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)


class WorkFlow(db.Model):
    """
    workflow
    """
    id = db.Column(db.Integer, primary_key=True)
    order_type_id = db.Column(db.Integer, db.ForeignKey('item_class.id'))
    order_status_id = db.Column(db.Integer, db.ForeignKey('status.id'))
    current_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    next_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    order_type = db.relationship('ItemClass', backref='workflow')
    order_status = db.relationship('Status', backref='status')

    current_user = db.relationship('User', backref='workflow_current_user', foreign_keys=[current_user_id])
    next_user = db.relationship('User', backref='workflow_next_user', foreign_keys=[next_user_id])


class FlowEngine(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    order_type = db.Column(db.String(16))
    operator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    operator = db.relationship('User', backref = db.backref('flows', lazy='dynamic'))
    status = db.Column(db.String(16))
    action = db.Column(db.String(16))
    previous_node_id = db.Column(db.Integer, db.ForeignKey('flow_engine.id'))
    next_node_id = db.Column(db.Integer, db.ForeignKey('flow_engine.id'))
    previous_node = db.relationship('FlowEngine', backref='previous_node', foreign_keys=[previous_node_id])
    next_node = db.relationship('FlowEngine', backref='next_node', foreign_keys=[next_node_id])